<template>
  <el-container class="home-container">
    <!--头部区-->
    <el-header>
      <div>
        <span>医院后台管理系统</span>
      </div>
      <el-dropdown>
      <span class="el-dropdown-link">
        <a href="javascript:;"><img src="./img.png" width="50px"></a>
      </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="enterCertificate"><a v-if="hasAuth('sys:user:certificate')" >医生信息认证</a></el-dropdown-item>
          <el-dropdown-item @click.native="$router.push('/setting/')"><a>修改密码</a></el-dropdown-item>
          <el-dropdown-item @click.native="logout"><a>退出登录</a></el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <!--页面主体区-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ?'64px':'200px'">
        <!-- 侧边栏菜单区 -->
        <div class="toggle-button" @click="toogleCollapse">|||</div>

        <el-menu
            :default-active="activePath"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            :collapse="isCollapse"
            active-text-color="#ffd04b">

          <router-link to="/home">
            <el-menu-item index="Index">
              <template slot="title">
                <i class="el-icon-s-home"></i>
                <span slot="title">首页</span>
              </template>
            </el-menu-item>
          </router-link>

          <el-submenu :index="menu.name" v-for="menu in menuList">
            <template slot="title">
              <i :class="menu.icon"></i>
              <span>{{menu.title}}</span>
            </template>

            <router-link :to="item.path" v-for="item in menu.children">
              <el-menu-item :index="item.name"  @click="saveNavstate(item.name)">
                <template slot="title">
                  <i :class="item.icon"></i>
                  <span slot="title">{{item.title}}</span>
                </template>
              </el-menu-item>
            </router-link>

          </el-submenu>

        </el-menu>

      </el-aside>
      <!--右侧内容-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import sysUserApi from "@/api/sysuser";
import {mapState} from "vuex"
import store from '@/store/'

export default {
  data(){
    return{
      isCollapse: false,
      activePath:'',
    }
  },
  computed:{
    ...mapState(['user']),
    menuList:{
      get() {
        return store.state.menuList
      }
    }
  },
  created() {
    // 组件被执行时就要赋值,window下储存的activePath赋值给新activePath
    this.activePath=window.sessionStorage.getItem('activePath')
  },
  methods:{
    enterCertificate(){
      this.$router.push("/certificate")
    },
    async logout(){
      const{data:res} = await sysUserApi.logout()

      if (res.code===200){
        //清除state中内容和localStorage中内容
        this.$store.commit('removeUser',null)
        await this.$router.push("/login");
      }else{
        return this.$message.error("退出失败")
      }
    },

    toogleCollapse(){
      this.isCollapse = !this.isCollapse
    },
    // 保持连接激活状态，激活选中的id
    saveNavstate(activePath){
      // 设置sessionStorage的activePath
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  }
}

</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  // 流动左右对齐
  justify-content:space-between;
  padding-left: 0;
  align-items: center;
  color: #ffffff;
  font-size: 20px;
  >div {
    display: flex;
    align-items: center;
    span{
      margin-left: 15px;
    }
  }
}
.el-aside
{
  background-color: #373d41;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: #EAEDF1;
}
.iconfont{
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.router-link-active{
  text-decoration: none;
}
a {
  text-decoration: none;
}
</style>